package de.nak.notendb.action;

import com.opensymphony.xwork2.Action;

import de.nak.notendb.model.ExaminationResult;
import de.nak.notendb.model.ExaminationSubject;
import de.nak.notendb.model.Maniple;
import de.nak.notendb.model.OralExaminationResult;
import de.nak.notendb.model.Student;

/**
 * Action to show the Protocol.
 * 
 * @author Christian Baumhof
 * 
 */
public class ShowProtocolAction implements Action {

	/**
	 * The saved ExaminationResult.
	 */
	private ExaminationResult result;

	/**
	 * The Maniple of the saved ExaminationResult.
	 */
	private Maniple maniple;

	/**
	 * The ExaminationSubject of the saved ExaminationResult.
	 */
	private ExaminationSubject subject;

	/**
	 * The Student of the of the saved ExaminationResult.
	 */
	private Student student;

	/**
	 * True, if the saved ExaminationResult has a valid OralExaminationResult.
	 */
	private Boolean hasOralExam;

	/**
	 * The (valid) OralExaminationResult of the saved ExaminationResult (null if
	 * there is no valid OralExaminationResult)
	 */
	@SuppressWarnings("unused")
	private OralExaminationResult oralExaminationResult;

	@Override
	public String execute() throws Exception {
		// Check result
		if (result == null) {
			return "DATA_NOT_FOUND";
		}

		// load student
		student = result.getStudent();

		// load maniple
		maniple = student.getManiple();

		// load subject
		subject = result.getExamination().getExaminationSubject();

		// load oral exam result
		hasOralExam = hasOralExam();
		if (hasOralExam) {
			oralExaminationResult = getValidOralExam();
		}

		return SUCCESS;
	}

	/**
	 * Returns the saved ExaminationResult.
	 * 
	 * @return an ExaminationResult.
	 */
	public ExaminationResult getResult() {
		return result;
	}

	/**
	 * Setter for the saved ExaminationResult
	 * 
	 * @param saved
	 *            ExaminationResult.
	 */
	public void setResult(ExaminationResult result) {
		this.result = result;
	}

	/**
	 * Returns the maniple.
	 * 
	 * @return a maniple.
	 */
	public Maniple getManiple() {
		return maniple;
	}

	/**
	 * Returns an ExaminationSubject.
	 * 
	 * @return an ExaminationSubject.
	 */
	public ExaminationSubject getSubject() {
		return subject;
	}

	/**
	 * Returns a student.
	 * 
	 * @return a student.
	 */
	public Student getStudent() {
		return student;
	}

	/**
	 * Returns true if there is a valid OralExaminationResult saved in the
	 * ExaminationResult result.
	 * 
	 * @return a Boolean.
	 */
	private Boolean hasOralExam() {
		if (result.getOralExaminationResults() == null) {
			for (OralExaminationResult oralResult : result
					.getOralExaminationResults()) {
				if (oralResult.isValid()) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * Returns a valid OralExaminationResult from the ExaminationResult result.
	 * If there is none, null is returned.
	 * 
	 * @return an OralExaminationResult or null
	 */
	private OralExaminationResult getValidOralExam() {
		for (OralExaminationResult oralResult : result
				.getOralExaminationResults()) {
			if (oralResult.isValid()) {
				return oralResult;
			}
		}
		return null;
	}

}
